package com.bankcomm.luafuction;

import java.util.Timer;
import java.util.TimerTask;

import org.keplerproject.luajava.LuaException;
import org.keplerproject.luajava.LuaObject;
import org.keplerproject.luajava.LuaState;

import android.app.Activity;

import com.bankcomm.common.ConfigManager;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.Utils;

public class LuaTimer {
	public static Timer timer_;

	public LuaTimer() {
		try {
			Component.LUASTATE.pushObjectValue(this);
			Component.LUASTATE.setGlobal("luatimer");
			String luahead = "timer={}";
			final String luafc1 = "function timer:startTimer(interval, repeats, run) local a1 = luatimer:startTimer(interval, repeats, run); return a1; end;";
			final String luafc2 = "function timer:stopTimer(timer) luatimer:stopTimer(timer); end;";
			Component.LUASTATE.gc(LuaState.LUA_GCCOLLECT, 0);
			Component.LUASTATE.LdoString(luahead.concat(luafc1).concat(luafc2));
		} catch (LuaException e) {
			// TODO Auto-generated catch block
			Utils.printException(e);
		}
	}
	

	/**
	 * 生成并启动一个定时器. interval：时间间隔（秒） repeats：是否重复；true-一直重复 false-不重复 run：计时器循环调用方法
	 */
	final public Timer startTimer(final float interval, final int repeats, final Object run) {
		// 如果频繁调用启动定时器，则先将已起的定时器取消
		if (null != timer_) {
			timer_.cancel();
		}
		timer_ = null;
		timer_ = new Timer();
		final boolean isCircle = repeats == 1 ? true : false;
		final boolean isSingle = repeats == 2 ? true : false;

		if (isCircle) {
			// 由于项目启用秒作单位，所以此处应X1000
			long intervalTemp = (long) (interval * 1000);
			// 启动一个计时器，并反复执行该任务
			timer_.scheduleAtFixedRate(new TimerTask() {

				@Override
				public void run() {
					if (run != null) {
						setParamsToLuaFuction((LuaObject) run);
					}
				}

			}, 0, intervalTemp);
		} else 
			if (isSingle) {
			// 由于项目启用秒作单位，所以此处应X1000
			long intervalTemp = (long) (interval * 1000);
			// 启动一个计时器，只执行一次
			timer_.schedule(new TimerTask() {
				
				@Override
				public void run() {
					if (run != null) {
						setParamsToLuaFuction((LuaObject) run);
					}
				}
			}, intervalTemp);
		} else {
			timer_.schedule(new TimerTask() {

				@Override
				public void run() {
					if (run != null) {
						setParamsToLuaFuction((LuaObject) run);
					}
				}

			}, 0);
		}

		return timer_;
	}

	final public void stopTimer(Object timer) {
		try {
			if (null != timer && timer instanceof Timer) {
				Timer t = (Timer) timer;
				t.cancel();
			}
		} catch (Exception e) {
			Utils.printException(e);
		}
	}

	/**
	 * 
	 * @param callback
	 *            需要执行的lua脚本方法
	 * @param param
	 *            需要传递的参数
	 */
	protected void setParamsToLuaFuction(final LuaObject callback) {
		if (ConfigManager.currentView_ != null) {
			ConfigManager.currentView_.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					if (callback.isFunction()) {
						// 如果是lua则将参数代入并执行并执行
						try {
							callback.call(new Object[] {}, 0);
						} catch (LuaException e) {
							Utils.printException(e);
						} 
					}
				}
			});
		}
	}
}
